import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Check {
	public static boolean checkPassword(String username, String pass) throws SQLException
	{
		String password = null;
		Connector conn = new Connector("select * from user where username = '" + username + "'");
		ResultSet rs = conn.execute();
		if(rs.first())
		{
			password = rs.getString("password");
			rs.close();		

			if(password.equals(pass))
				return true;
			else
				return false;
		}
		else
		{
			rs.close();
			return false;
		}
	}

	public static boolean checkUsername(String user) throws SQLException {
		Connector conn = new Connector("select * from user where username = '"
				+ user + "'");
		ResultSet rs = conn.execute();
		if (rs.next()) {
			rs.close();
			return true;
		} else {
			rs.close();
			return false;
		}
	}

	public static boolean checkUserID(int uid) throws SQLException {
		Connector conn = new Connector("select * from user where userid = '"
				+ uid + "'");
		ResultSet rs = conn.execute();
		if (rs.next()) {
			rs.close();
			return true;
		} else {
			rs.close();
			return false;
		}
	}

	public static boolean checkFacility(String fn) throws SQLException {
		Connector conn = new Connector(
				"select * from facility where facilityname = '" + fn + "'");
		ResultSet rs = conn.execute();
		if (rs.next()) {
			rs.close();
			return true;
		} else {
			rs.close();
			return false;
		}
	}

	public static boolean checkFacilityID(int fid) throws SQLException {
		Connector conn = new Connector(
				"select * from facility where facilityid = '" + fid + "'");
		ResultSet rs = conn.execute();
		if (rs.next()) {
			rs.close();
			return true;
		} else {
			rs.close();
			return false;
		}
	}

	public static boolean checkReserveID(int rid) throws SQLException {
		Connector conn = new Connector(
				"select * from reserve where reserveid = '" + rid + "'");
		ResultSet rs = conn.execute();
		if (rs.next()) {
			rs.close();
			return true;
		} else {
			rs.close();
			return false;
		}
	}

	public static boolean checkVenue(String vn) throws SQLException {
		Connector conn = new Connector(
				"select * from venue where venuename = '" + vn + "'");
		ResultSet rs = conn.execute();
		if (rs.next()) {
			rs.close();
			return true;
		} else {
			rs.close();
			return false;
		}
	}

	public static boolean checkVenueID(int vid, int fid) throws SQLException {
		Connector conn = new Connector(
				"select * from venue where venueid = '" + vid + "' and facilityid = '" + fid + "'");
		ResultSet rs = conn.execute();
		if (rs.next()) {
			rs.close();
			return true;
		} else {
			rs.close();
			return false;
		}
	}

	public static boolean checkBookingID(int bid) throws SQLException {
		Connector conn = new Connector(
				"select * from booking where bookingid = '" + bid + "'");
		ResultSet rs = conn.execute();
		if (rs.next()) {
			rs.close();
			return true;
		} else {
			rs.close();
			return false;
		}
	}

	public static boolean checkAvailable(int vid, Date da) throws SQLException {
		int count = 0;
		Timestamp ds = new Timestamp(da.getTime());
		String sql = "select venue.venuename,(select count(*) from booking where venueid = venue.venueid and bookingstart = '"
						+ ds + "') from venue where venueid = " + vid;
		Connector conn = new Connector(sql);
		ResultSet rs = conn.execute();
		if (rs.next()) {
			count = rs.getInt(2);
			rs.close();
		} else {
			rs.close();
		}

		DateFormat format1 = new SimpleDateFormat("yyyy-MM-dd");
		Connector conn1 = new Connector("select * from reserve where reservedate='" + format1.format(da) + "'");
		ResultSet rs1 = conn1.execute();
		if (rs1.next()) {
			rs1.close();
			return false;
		} else {
			rs1.close();
		}

		Venue ve = new Venue(vid);
		int cap = ve.getCapacity();

		if (count < cap)
			return true;
		else
			return false;
	}
}
